package cn.gs.serializable.serialize;

// @author gs
import cn.gs.serializable.entity.IEntity;
import cn.gs.serializable.entity.OneEntity;

public class StringSerialize implements ISerialize {

    @Override
    public <T> T toObj(cn.gs.serializable.serialize.ISerialize ctrl, IEntity from, Class<T> clazz) {
        if (!(from instanceof OneEntity) || !((OneEntity) from).isQuote()) {
            return null;
        }
        String t = ((OneEntity) from).getValue();
        if (clazz == null || clazz.isAssignableFrom(String.class)) {
            return (T) t;
        } else if (clazz.isAssignableFrom(Character.class) || clazz == char.class) {
            if (t.length() > 0) {
                return (T) Character.valueOf(t.charAt(0));
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public IEntity fromObj(ISerialize ctrl, Object o) {
        if (o == null) {
            return null;
        }
        if (o instanceof Character || o instanceof String) {
            return new OneEntity(o.toString(), true);
        } else {
            return null;
        }
    }
}
